﻿/*******************************************************
 *  单词列表 相关操作js
 * <p>Title: wordMeanManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2019年8月6日 下午5:41:57
 * @version 1.0
 * *****************************************************/
var $table, unitzTree, options;//树控件
var wordMeanManager = {
    //1.0 初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        wordMeanManager.bindEvent();
        wordMeanManager.initTable();
        parent.delLoadBoxs();
    },
    //1.1 初始化Table
    initTable: function () {
        options = {
            url: ctx + "/HeadManager/InitWordSenseTable",
            cutHeight: 5,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            singleSelect: false,//多选
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    keyword: $('#keyword').val()
                }
                return params;
            },
            columns:[
            {
                checkbox: true
            },{
                field: '',
                title: '序号',
                align: 'center',
                width: 50,
                formatter: function (value, row, index) {                     
                    return index + 1;
                }
            },{
                field: 'word',
                title: '单词名称',
                align: 'center',
                width: 100
            }, {
                field: 'gj_symbol',
                title: '国际音标',
                align: 'center',
                width: 100
            }, {
                field: 'kk_symbol',
                title: 'KK音标',
                align: 'center',
                width: 100
            }, {
                field: 'cp_type',
                title: '词性',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            }, {
                field: 'china_mean',
                title: '中文释义',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            },  {
                field: 'ws_phrase',
                title: '短语',
                align: 'center',
                width: 120,
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            }, {
                field: 'example',
                title: '例句',
                align: 'center',
                width: 120,
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            }, {
                field: 'update_name',
                title: '最后修改人',
                align: 'center',
                width: 80
            }, {
                field: 'update_date',
                title: '最后修改时间',
                align: 'center',
                width: 100
            }, {
                field: 'create_name',
                title: '创建人',
                align: 'center',
                width: 80
            }, {
                field: 'create_date',
                title: '创建时间',
                align: 'center',
                width: 100
            }],
            onClickRow: function (row, $element) {
            },
            onLoadSuccess: function (data) {
            }
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //1.2 事件绑定
    bindEvent: function () {
        //初始化file file-pretty
        $('.files').prettyFile();
        //查询
        $('#btnSearch').on('click',function () {
            $table.bootstrapTable('refresh', options);
        });
        //新增
        $('#btnAdd').on('click', function () {
            wordMeanManager.editForm();
        });
        //编辑
        $('#btnEdit').on('click', function () {
            var row = wordMeanManager.selections();
            if (!row)
                return false;
            wordMeanManager.editForm(row);
        });
        //删除
        $('#btnDelete').on('click', function () {
            var row = wordMeanManager.selections(true);
            if (!row)
                return false;
            var ids = new Array();
            $.each(row, function (index,item) {
                ids.push(item.ws_id);
            });
            layer.confirm('您确定要删除当前选中单词？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                $.ajax({
                    type: 'post',
                    url: ctx + '/HeadManager/DelWordById',
                    dataType: 'json',
                    data: { ids: ids.join(",") },
                    async: false,
                    success: function (data) {
                        if (index)
                            layer.close(index);
                        if (data.Statu === 0) {
                            layer.msg("删除成功...");
                            $table.bootstrapTable('refresh', options);
                        }
                        else
                            layer.msg(data.Msg, { icon: 5 });
                    }
                });
            }, function () { });
        });
        //导入
        $('#btnImportAdd').on('click', function () {
            //清空表单
            $('#importFormTable').resetHideValidForm();
            //打开导入
            layer.open({
                title: ['导入单词', 'font-size:18px;'],
                type: 1,
                content: $('#import_form'),
                scroll: true,//是否显示滚动条、默认不显示
                btn: ['确定', '取消'],
                area: ['480px', '300px'],
                yes: function (li, o) {
                    //判断文件
                    var fileSize = $("#excelFile")[0].files;
                    if (fileSize.length <= 0) {
                        layer.alert('请选择导入文件！', { icon: 5 });
                        return false;
                    }
                    //验证通过
                    $('#importFormTable').ajaxSubmit({
                        url: ctx + "/HeadManager/ImportWordSense",
                        type: "post",
                        dataType: "json",
                        beforeSubmit: function (arr, $form, options) {
                            layer.msg("提交数据~", { icon: 1, time: 5000 });
                        },
                        success: function (result, status, xhr, $form) {
                            if (result.Statu === 0) {
                                layer.closeAll();
                                $table.bootstrapTable('refresh', options);
                                layer.alert(result.Msg, { icon: 1 });
                            }
                            else {
                                if (result.Data) {
                                    var str = "";
                                    for (var i = 0; i < result.Data.length; i++) {
                                        str += result.Data[i] + "、";
                                    }
                                    layer.alert(str, { icon: 5 });
                                } else
                                    layer.alert(result.Msg, { icon: 5 });
                            }
                        },
                        error: function (xhr, status, error, $form) {
                            layer.msg("保存失败~", { icon: 5 });
                        }
                    });
                },
                cancel: function (li, o) {

                }
            });
        });
    },
    //1.3 获取被选中节点
    selections: function (isAll) {
        var row = $table.bootstrapTable('getSelections');
        if (!row || row.length === 0) {
            layer.msg("数据为空或未选中记录行~", { icon: 5 });
            return false;
        }
        if (isAll)
            return row;
        if (row.length > 1) {
            layer.msg("当前操作不支持多选，请选择一行再操作！", { icon: 5 });
            return false;
        }
        return row[0];
    },
    //1.6 添加、编辑
    editForm: function (row) {
        var $title = "添加关卡单词";
        if (!row)
            addOrEditUrl = ctx + "/HeadManager/AddWord";//新增
        else {
            addOrEditUrl = ctx + "/HeadManager/EditWord";//编辑
            $title = "编辑关卡单词";
        }
        //清空表单
        $('#orgFormTable').resetHideValidForm();
        //打开编辑
        layer.open({
            title: [$title, 'font-size:18px;'],
            type: 1,
            content: $('#org_form'),
            scroll: true,//是否显示滚动条、默认不显示
            btn: ['确定', '取消'],
            area: ['480px', '98%'],
            success: function (li, o) {
                if (row != null) {
                    $('#orgFormTable')[0].reset();//重置表单
                    $('#orgFormTable').LoadForm(row);//表单填充数据
                }
            },
            yes: function (li, o) {
                if ($('#orgFormTable').valid()) {
                    //验证通过
                    $('#orgFormTable').ajaxSubmit({
                        url: addOrEditUrl,
                        type: "post",
                        dataType: "json",
                        beforeSubmit: function (arr, $form, options) {
                            layer.msg("提交数据~", { icon: 1, time: 5000 });
                        },
                        success: function (result, status, xhr, $form) {
                            if (result.Statu === 0) {
                                layer.closeAll();
                                $table.bootstrapTable('refresh', options);
                                layer.msg('保存成功', { icon: 1 });
                            }
                            else
                                layer.alert(result.Msg, { icon: 5 });
                        },
                        error: function (xhr, status, error, $form) {
                            layer.msg("保存失败~", { icon: 5 });
                        }
                    });
                }
            },
            cancel: function (li, o) {

            }
        });
    }
}